$(function() {
    // topY 距离多少执行
    $(document).on('animateEleScroll', function(e, ele, animateClass, topY ) {
        if (ele.length  == 0) return;

        var $window = $(window);
        var $ele = ele;

        if ( $ele.offset().top <= $window.height()) {
            $ele.addClass(animateClass);
        }

        var top = (topY != undefined && topY) ? topY : 0;
    
        $window.on('scroll', function () {
            var sh = $window.scrollTop() + $window.height() + top;
    
            if ( $ele.offset().top <= sh && !$ele.hasClass(animateClass)) {
                $ele.addClass(animateClass);
            }
        });
    });


    // 添加商品到购物车
    $(document).on('addCart', function (e, gid) {
        $.post(G_BASE_URL + '/cart/ajax/add_cart/', 'gid=' + gid, function (data) {
            if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ) {
                if(data.errno == 1) {
                    alert('收藏商品成功');
                } else {
                    alert('收藏商品失败，' + data.err);
                }
            } else {
                if(data.errno == 1) {
                    $.Toast("", "收藏商品成功", "success", {
                        stack: true,
                        has_icon:true,
                        has_close_btn:true,
                        fullscreen:false,
                        timeout:2000,
                        sticky:false,
                        has_progress:true,
                        rtl:false,
                    });
                } else {
                    $.Toast("提示", '收藏商品失败，' + data.err, "error", {
                        stack: true,
                        has_icon:true,
                        has_close_btn:true,
                        fullscreen:false,
                        timeout:2000,
                        sticky:false,
                        has_progress:true,
                        rtl:false,
                    });
                }
            }
           
        }, 'json');
    });

    // 购物车删除商品
    $(document).on('delCart', function (e, gid, fn) {
        $.post(G_BASE_URL + '/cart/ajax/del_cart/', 'gid=' + gid, function (data) {
            if(data.errno == 1) {
                $.Toast("", "删除商品成功", "success", {
                    stack: true,
                    has_icon:true,
                    has_close_btn:true,
                    fullscreen:false,
                    timeout:2000,
                    sticky:false,
                    has_progress:true,
                    rtl:false,
                });
                fn();
            } else {
                $.Toast("提示", "删除商品失败", "error", {
                    stack: true,
                    has_icon:true,
                    has_close_btn:true,
                    fullscreen:false,
                    timeout:2000,
                    sticky:false,
                    has_progress:true,
                    rtl:false,
                });
            }
        }, 'json');
    });

    // 切换颜色
    $(document).on('changeShopColor', function(e, $this ) {

        $this.parents('.pro-li-label').find('li').addClass('no-active');

        $this.parents('.pro-li-label').find('li').each(function(k, d) {
            if ($(d).data('color') == $this.data('color')){
                $(d).removeClass('no-active');
            }
        });


        $this.parents('.pro-li-body').find('.img-block img').attr('src', $this.data('src'));

    });

    // 暂停/播放
    $(document).on('togglePlay', function(e, $this, av) {
        if (av.paused) {
            av.play();
            $this.find('.am-play-img').addClass('hide');
            $this.find('.am-pause-img').removeClass('hide');
        } else {
            av.pause();
            $this.find('.am-pause-img').addClass('hide');
            $this.find('.am-play-img').removeClass('hide');
        }
    });


    $('.pro-li-label').on('click', 'li', function (e) {
        $(document).trigger('changeShopColor', [ $(this)]);
    });



    $('.pro-li-body > .img-block').on('click', function(e) {
        window.location.href = '//' + window.location.host + $(this).data('src');
    });

    var av = document.getElementById("am-video1");
    var av2 =  document.getElementById("am-video2");
    var av3 =  document.getElementById("am-video3");

    $('#am-play, #am-play2, #am-play3').on('click', function(e) {
        var tmpAv = av;
        switch($(this).attr('id')) {
            case 'am-play2':
                tmpAv = av2;
                break;
            case 'am-play3':
                tmpAv = av3;
                break;
        }
        $(document).trigger('togglePlay', [$(this), tmpAv]);
    });
    
    $(document).trigger('animateEleScroll', [$('#bn-logo'), 'opacity-fade']);
});